package com.mdd.front.service.impl.yangyangLeadingEnterprise;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.yangyangLeadingEnterprise.TAppModule;
import com.mdd.common.entity.yangyangLeadingEnterprise.common.JsonReturn;
import com.mdd.common.mapper.yangyangLeadingEnterprise.TAppModuleMapper;
import com.mdd.common.util.TimeUtils;
import com.mdd.front.service.yangyangLeadingEnterprise.ITAppModuleService;
import com.mdd.front.validate.common.PageValidate;
import com.mdd.front.validate.yangyangLeadingEnterprise.TAppModuleCreateValidate;
import com.mdd.front.validate.yangyangLeadingEnterprise.TAppModuleSearchValidate;
import com.mdd.front.validate.yangyangLeadingEnterprise.TAppModuleUpdateValidate;
import com.mdd.front.vo.yangyangLeadingEnterprise.TAppModuleDetailVo;
import com.mdd.front.vo.yangyangLeadingEnterprise.TAppModuleListedVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.*;

/**
 * app导航模块实现类
 * @author LikeAdmin
 */
@Service
public class TAppModuleServiceImpl implements ITAppModuleService {
        
    @Resource
    TAppModuleMapper tAppModuleMapper;

    /**
     * app导航模块列表
     *
     * @author LikeAdmin
     * @param pageValidate 分页参数
     * @param searchValidate 搜索参数
     * @return PageResult<TAppModuleListedVo>
     */
    @Override
    public PageResult<TAppModuleListedVo> list(PageValidate pageValidate, TAppModuleSearchValidate searchValidate) {
        Integer page  = pageValidate.getPage_no();
        Integer limit = pageValidate.getPage_size();

        QueryWrapper<TAppModule> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc(Arrays.asList("sort", "id"));

        tAppModuleMapper.setSearch(queryWrapper, searchValidate, new String[]{
            "like:name:str",
            "=:sort:int",
            "=:type:int",
            "=:clickFun@click_fun:str",
        });

        IPage<TAppModule> iPage = tAppModuleMapper.selectPage(new Page<>(page, limit), queryWrapper);

        List<TAppModuleListedVo> list = new LinkedList<>();
        for(TAppModule item : iPage.getRecords()) {
            TAppModuleListedVo vo = new TAppModuleListedVo();
            BeanUtils.copyProperties(item, vo);
            vo.setCreateTime(TimeUtils.timestampToDate(item.getCreateTime().getTime()));
            vo.setUpdateTime(TimeUtils.timestampToDate(item.getUpdateTime().getTime()));
            list.add(vo);
        }

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    /**
     * app导航模块详情
     *
     * @author LikeAdmin
     * @param id 主键参数
     * @return TAppModule
     */
    @Override
    public TAppModuleDetailVo detail(Integer id) {
        TAppModule model = tAppModuleMapper.selectOne(
                new QueryWrapper<TAppModule>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在");

        TAppModuleDetailVo vo = new TAppModuleDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * app导航模块新增
     *
     * @author LikeAdmin
     * @param createValidate 参数
     */
    @Override
    public void add(TAppModuleCreateValidate createValidate) {
        TAppModule model = new TAppModule();
        model.setName(createValidate.getName());
        model.setSort(createValidate.getSort());
        model.setType(createValidate.getType());
        model.setClickFun(createValidate.getClickFun());
//        model.setCreateTime(System.currentTimeMillis() / 1000);
//        model.setUpdateTime(System.currentTimeMillis() / 1000);
        model.setCreateTime(new Date());
        model.setUpdateTime(new Date());
        tAppModuleMapper.insert(model);
    }

    /**
     * app导航模块编辑
     *
     * @author LikeAdmin
     * @param updateValidate 参数
     */
    @Override
    public void edit(TAppModuleUpdateValidate updateValidate) {
        TAppModule model = tAppModuleMapper.selectOne(
                new QueryWrapper<TAppModule>()
                    .eq("id",  updateValidate.getId())
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setId(updateValidate.getId());
        model.setName(updateValidate.getName());
        model.setSort(updateValidate.getSort());
        model.setType(updateValidate.getType());
        model.setClickFun(updateValidate.getClickFun());
//        model.setUpdateTime(System.currentTimeMillis() / 1000);
        model.setUpdateTime(new Date());
        tAppModuleMapper.updateById(model);
    }

    /**
     * app导航模块删除
     *
     * @author LikeAdmin
     * @param id 主键ID
     */
    @Override
    public void del(Integer id) {
        TAppModule model = tAppModuleMapper.selectOne(
                new QueryWrapper<TAppModule>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        tAppModuleMapper.delete(new QueryWrapper<TAppModule>().eq("id", id));
    }

    /**
     * app导航模块查询
     * @param type
     * @return
     */
    @Override
    public String selectList(Integer type) {
        List<TAppModule> appModuleList = tAppModuleMapper.selectList(new QueryWrapper<TAppModule>().eq("type", type).orderByAsc("sort"));
        return JsonReturn.success().setListData(Optional.ofNullable(appModuleList).orElse(new ArrayList<>())).toJsonStr();
    }

}
